#include "ChatManager.h"
#include "Character/Player.h"
#include "LogicService.h"
#include "Common/TableData/ChatChatCfg.h"
#include "Character/PlayerMgr.h"

ChatManager::ChatManager()
{

}

ChatManager::~ChatManager()
{

}

bool ChatManager::Init()
{
	return true;
}

bool ChatManager::UnInit()
{
	return true;
}

void ChatManager::InitPlayerMiniInfoStruct(Player* pPlayer, ProtoBuf::CharacterPlayerMiniInfo& info)
{
	if (NULL == pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] ChatManager::InitPlayerMiniInfoStruct but Player is nullptr");
		return;
	}
	
	info.set_playerid(pPlayer->GetId());
	info.set_playername(pPlayer->GetName());
	info.set_sex(pPlayer->GetAttr(C_GENDER));
	info.set_level(pPlayer->GetAttr(C_LEVEL));
	info.set_prof(pPlayer->GetAttr(C_PROF));
	info.set_camp(pPlayer->GetAttr(C_CAMP_ID));
}

void ChatManager::SendG2CChat(uint32_t cmd, std::vector<CharIDType>& playerIdList, ProtoBufBase* msg)
{
	for (uint32_t i = 0; i < playerIdList.size(); i++)
	{
		Player* pPlayer = g_GetCreatureMgr()->GetPlayer(playerIdList[i]);
		if (nullptr == pPlayer)
		{
			MMOLOG_FMT_ERROR("[logic] GMailManager::SendG2CChat but Player is nullptr charId:%u", playerIdList[i]);
			continue;
		}

		pPlayer->SendDataToClient(EMODULE_ID_CHAT, cmd, msg);
	}
}

void ChatManager::SendSysG2W(uint32_t cmd, ProtoBufBase* msg)
{
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_SERV_SYS, cmd, msg);
}

void ChatManager::SendBroadcastChat(BROADCAST_TYPE id, SystemChatMsgData& msgData, std::vector<CharIDType>& playerIdList)
{
	const ChatChatCfgInfo *pCfg = g_GetChatChatCfgTable()->GetChatChatCfgInfo(id);
	if (nullptr == pCfg)
	{
		LogErrFmtPrint("[logic] ChatManager::SendBroadcastChat ... nullptr == pCfg...nBroadcastChatID:%u ", id);
		return;
	}

	GCSystemChatMsgNotify msg;

	if (pCfg->chatType < CHAT_TYPE_NONE || pCfg->chatType > CHAT_TYPE_LIMIT)
	{
		LogErrFmtPrint("[logic] ChatManager::SendBroadcastChat ...pCfg chatType is error .nBroadcastChatID:%u ", id);
		return;
	}

	msg.set_chattype(CHAT_TYPE(pCfg->chatType));
	msg.set_msgtype(id);

	//填充角色信息
	for (uint32_t i = 0; i < msgData.players.size(); i++)
	{
		msg.add_players()->CopyFrom(msgData.players[i]);
	}

	//填充text
	for (uint32_t i = 0; i < msgData.text.size(); i++)
	{
		msg.add_text(msgData.text[i]);
	}

	//填充parmas
	for (uint32_t i = 0; i < msgData.params.size(); i++)
	{
		msg.add_parmas(msgData.params[i]);
	}

	SendG2CChat(LOGIC_TO_CLIENT_SYSTEMCHATMSGNOTIFY, playerIdList, &msg);
}


void ChatManager::SendG2WBroadcast(BROADCAST_TYPE id, SystemChatMsgData& msgData, uint32_t extraId)
{
	ProtoBuf::GWSysChatMsgNotify notifyMsg;

	const ChatChatCfgInfo *pCfg = g_GetChatChatCfgTable()->GetChatChatCfgInfo(id);
	if (nullptr == pCfg)
	{
		LogErrFmtPrint("[logic] ChatManager::SendG2WBroadcast ... nullptr == pCfg...nBroadcastChatID:%u ", id);
		return;
	}

	if (pCfg->chatType < CHAT_TYPE_NONE || pCfg->chatType > CHAT_TYPE_LIMIT)
	{
		LogErrFmtPrint("[logic] ChatManager::SendG2WBroadcast ...pCfg chatType is error .nBroadcastChatID:%u ", id);
		return;
	}

	notifyMsg.set_chattype(CHAT_TYPE(pCfg->chatType));
	notifyMsg.set_msgtype(id);

	//填充角色信息
	for (uint32_t i = 0; i < msgData.players.size(); i++)
	{
		notifyMsg.add_players()->CopyFrom(msgData.players[i]);
	}

	//填充text
	for (uint32_t i = 0; i < msgData.text.size(); i++)
	{
		notifyMsg.add_text(msgData.text[i]);
	}

	//填充parmas
	for (uint32_t i = 0; i < msgData.params.size(); i++)
	{
		notifyMsg.add_parmas(msgData.params[i]);
	}

	notifyMsg.set_extraid(extraId);

	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CHAT, CENTER_SERVER_PROTOCOL_GWSYSCHATMSGNOTIFY, &notifyMsg);
}


